package com.zlsy.thread.sixstates;

/**
 * @author zhouliang
 * @date 2020/2/27 18:42
 * @Desc 展示线程的三种状态：NEW  RUNNABLE TERMINATED
 */
public class NewRunableTerminated implements Runnable {

    public static void main(String[] args) {
        Thread thread = new Thread(new NewRunableTerminated());
        //线程刚刚创建 ====>>> NEW
        System.out.println(thread.getState());
        thread.start();
        // 线程启动：有可能还没拿到CPU资源，也有可能已经拿到了CPU资源 ====>>> RUNNABLE
        System.out.println(thread.getState());
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //选择运行5毫秒是因为我们希望看到打印过程中线程的状态 ======>>> RUNNABLE
        System.out.println(thread.getState());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //最后子线程运行结束 查看线程的状态 ===== >>> TERMINATED
        System.out.println(thread.getState());
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(i);
        }
    }
}
